﻿@using MudBlazor.Utilities

<MudPopoverProvider />

<MudColorPicker @ref="_picker"
                @bind-Value="ColorValue"
                TextChanged="@OnTextChanged"
                Palette="@Palette"
                ShowToolbar="@ShowToolbar"
                ShowTooltips="@ShowTooltips"
                ShowColorField="@ShowColorField"
                ShowPreview="@ShowPreview"
                ShowSliders="@ShowSliders"
                ShowInputs="@(!DisableInput)"
                ShowModeSwitch="@ShowModeSwitch"
                ShowAlpha="@ShowAlpha"
                DragEffect="@DragEffect"
                ColorPickerView="@ViewMode"
                ColorPickerMode="@ColorPickerMode"
                PickerVariant="@Variant"
                Required="@Required"
                InputId="@InputId" />

@code {
    private MudColorPicker _picker = null!;

    public int ValueChangeCallbackCounter { get; private set; }

    [Parameter]
    public MudColor ColorValue { get; set; } = "#594ae2";

    [Parameter]
    public string? TextValue { get; set; } = "#594ae2";

    [Parameter]
    public IEnumerable<MudColor> Palette { get; set; } = ["#ff4081ff", "#2196f3ff", "#00c853ff", "#ff9800ff", "#f44336ff"];

    [Parameter]
    public ColorPickerMode ColorPickerMode { get; set; } = ColorPickerMode.RGB;

    [Parameter]
    public bool ShowToolbar { get; set; }

    [Parameter]
    public bool ShowColorField { get; set; } = true;

    [Parameter]
    public bool ShowPreview { get; set; } = true;

    [Parameter]
    public bool ShowSliders { get; set; } = true;

    [Parameter]
    public bool DisableInput { get; set; }

    [Parameter]
    public bool ShowModeSwitch { get; set; } = true;

    [Parameter]
    public bool ShowTooltips { get; set; } = true;

    [Parameter]
    public bool ShowAlpha { get; set; } = true;

    [Parameter]
    public bool DragEffect { get; set; } = true;

    [Parameter]
    public ColorPickerView ViewMode { get; set; } = ColorPickerView.Spectrum;

    [Parameter]
    public PickerVariant Variant { get; set; } = PickerVariant.Static;

    [Parameter]
    public bool Required { get; set; }

    [Parameter]
    public string? InputId { get; set; }

    public Task OpenPicker()
    {
        return InvokeAsync(async () =>
        {
            await _picker.OpenAsync();
        });
    }

    public Task ClosePicker()
    {
        return InvokeAsync(async () =>
        {
            await _picker.CloseAsync();
        });
    }
      
    private void OnTextChanged(string? value)
    {
        TextValue = value;
        ValueChangeCallbackCounter++;
    }
}

